#include "CMainWindow.h"
#include "../Common/CCommonFunction.cpp"
#include "../Common/CMyWindow.h"
#include "ui_CMainWindow.h"

#include <QDomDocument>

const QString FILE_NAME = "MyTool.xml";
const QString MODULE_NAME = "MyTool";
const QString SELECT_ATTR_TAG = "select";

QString CMainWindow::s_strFileName = QString();

CMainWindow::CMainWindow(QWidget* parent)
    : QMainWindow(parent),
      ui(new Ui::CMainWindow)
{
    ui->setupUi(this);

    // 初始化系统图标
    CComponentBridge::GetInstance()->InitSystemTrayIcon(this);

    // 设置窗口关闭时不释放
    setAttribute(Qt::WA_DeleteOnClose, false);

    // 文件名获取
    CCommonFunction::GetDataSavePath(s_strFileName);
    s_strFileName += FILE_NAME;

    // 给状态栏设置一个Label用于显示快捷方式路径
    m_lbStatus = new QLabel(this);
    ui->statusbar->addWidget(m_lbStatus);

    // 加载文件
    LoadFile();

    // 信号槽连接
    connect(ui->widgetMyShortcut, SIGNAL(signalSetStatusText(const QString&)), this, SLOT(slotStatusShowText(const QString&)));
    connect(ui->widgetMyShortcut, SIGNAL(signalClose()), this, SLOT(close()));

    // 按钮名
    ui->btnMyShortcut->setText(ui->widgetMyShortcut->windowTitle());
    ui->btnCopyFile->setText(ui->widgetCopyFile->windowTitle());
    ui->btnTranslate->setText(ui->widgetTranslate->windowTitle());
}

CMainWindow::~CMainWindow()
{
    // 保存大小
    ((CMyWindow*)ui->stackedWidget->currentWidget())->SaveSize(this);

    SaveFile();

    delete ui;
}

void CMainWindow::slotStatusShowText(const QString& text)
{
    m_lbStatus->setText(text);
}

void CMainWindow::on_btnMyShortcut_clicked()
{
    CMyWindow* pWindow = (CMyWindow*)ui->widgetMyShortcut;
    OnComponentChange(pWindow->GetComponentId());
}

void CMainWindow::on_btnCopyFile_clicked()
{
    CMyWindow* pWindow = (CMyWindow*)ui->widgetCopyFile;
    OnComponentChange(pWindow->GetComponentId());
}

void CMainWindow::on_btnTranslate_clicked()
{
    CMyWindow* pWindow = (CMyWindow*)ui->widgetTranslate;
    OnComponentChange(pWindow->GetComponentId());
}

void CMainWindow::SaveFile()
{
    QFile file(s_strFileName);

    QDomDocument doc;

    // xml 根
    QDomElement root;
    if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
        // 创建 xml 头部
        QDomProcessingInstruction instruction;
        instruction = doc.createProcessingInstruction("xml", "version = \'1.0\' encoding=\'UTF-8\'");
        doc.appendChild(instruction);

        // 添加根
        root = doc.createElement(MODULE_NAME);
        doc.appendChild(root);

        // 设置属行
        root.setAttribute(SELECT_ATTR_TAG, ui->stackedWidget->currentIndex());

        // 保存信息到文件
        QTextStream out(&file);
        doc.save(out, 4);

        file.close();
    }
}

void CMainWindow::LoadFile()
{
    QFile file(s_strFileName);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        return;
    }

    QDomDocument doc;
    QString error = "";
    int row = 0, column = 0;

    // 关联 xml 与 文件
    if (!doc.setContent(&file, false, &error, &row, &column)) {
        file.close();
        return;
    }

    QDomElement root = doc.documentElement();
    int index = root.attribute(SELECT_ATTR_TAG).toInt();
    switch (index) {
        case 0:
            on_btnMyShortcut_clicked();
            break;

        case 1:
            on_btnCopyFile_clicked();
            break;

        case 2:
            on_btnTranslate_clicked();
            break;

        default:
            break;
    }
}

void CMainWindow::OnComponentChange(int index)
{
    // 情况状态栏
    slotStatusShowText("");

    // 将已存在的 menubar 隐藏
    if (this->menuBar() != nullptr) {
        this->menuBar()->hide();
    }

    // 获取当前功能的菜单栏
    CMyWindow* pWindow = (CMyWindow*)ui->stackedWidget->widget(index);
    QMenuBar* pMenuBar = pWindow->GetMenuBar();
    if (pMenuBar != nullptr) {
        pMenuBar->setParent(this);
        pMenuBar->show();
        this->setMenuBar(pMenuBar);
    }

    ui->stackedWidget->setCurrentIndex(pWindow->GetComponentId());

    // 重置大小
    pWindow->LoadSize(this);

    this->setWindowTitle(pWindow->windowTitle());
    this->setMinimumSize(pWindow->minimumSize());
    CComponentBridge::GetInstance()->SetSystemIconToolTip(this->windowTitle());
}
